import sys

dict = {}
sod = {}
ad = {}
f = open(sys.argv[1])

line = f.readline()
while line:
    line = f.readline()
    tk = line.split(' ')
    if len(tk) < 4:
        continue

    if tk[2] == '+':
        if len(tk) < 5:
            #sys.stderr.write("invalid line: %s\n" % line)
            continue
        if tk[3].strip() == "-":
            continue

        if not tk[4].strip().startswith("0x"):
            continue

        if tk[1] in ad:
            ad[tk[1]] = ad[tk[1]] + 1
        else:
            ad[tk[1]] = 1
        dict[tk[3].strip()] = {tk[1] : tk[4].strip()}

    elif tk[2] == '-':
        if tk[3].strip() in dict:
            del dict[tk[3].strip()]
f.close()

for key,value in dict.items():

    for so,size in value.items():
        if so in sod:
            sod[so] = sod[so] + int(size, 16)
        else:
            sod[so] = int(size, 16)

        print(key + " @ " + so + " : " + size)


sys.stderr.write("####################################################\n")
sys.stderr.write("Total memory consumption of each so file:\n")
sys.stderr.write("####################################################\n\n")

for so,total in sorted(sod.items(), key=lambda x: x[1]):
    sys.stderr.write("{} : {} \n".format(total, so))


sys.stderr.write("\n")
sys.stderr.write("####################################################\n")
sys.stderr.write("so file memory alloc times:\n")
sys.stderr.write("####################################################\n\n")

for so,count in sorted(ad.items(), key=lambda x: x[1]):
    sys.stderr.write("{} : {} \n".format(count, so))
